import Vue from "vue";
Vue.config.productionTip = false;

import App from "./App.vue";

import ElementUI from "element-ui";
import "element-ui/lib/theme-chalk/index.css";
Vue.use(ElementUI);

import jQuery from "jquery";
Vue.prototype.$query = window.jQuery = jQuery;

/* plugins.setFontSize(16);

window.addEventListener("resize", () => {
    plugins.setFontSize(16);
}); */

const storeMod = () =>
    import ( /* webpackChunkName:'vue-store-modules' */ "./plugins/modules"),
    plugins = () =>
    import ( /* webpackChunkName:'vue-plugins' */ "./plugins/extend"),
    pluginsApi = () =>
    import ( /* webpackChunkName: 'vue-plugins-api' */ "./plugins/api"),
    VueRouter = () =>
    import ( /* webpackChunkName: 'vue-router' */ "./router/router"),
    VueStore = () =>
    import ( /* webpackChunkName: 'vue-store' */ "./store/store");

Promise.all([
    VueRouter(),
    VueStore(),
    storeMod(),
    plugins(),
    pluginsApi(),
]).then((v) => {
    let router = v[0].default,
        store = v[1].default,
        storeModules = v[2].modules;

    storeModules(Vue, store)
        .then(() => {
            Vue.prototype.$Plugins = v[3].default;
            Vue.$api = Vue.prototype.$api = v[4].default;
            store.state.api = v[4].default;

            router.beforeEach((to, from, next) => {
                console.log(to, from);
                to.meta && to.meta.title && jQuery("head>title").html(to.meta.title);
                if (!Vue.$token.has() && !/\/login/.test(to.path)) {
                    router.push("/login");
                } else {
                    next();
                }
            });

            router.afterEach((to, from) => {
                console.log(to, from);
            });

            new Vue({
                router,
                store,
                render: (h) => h(App),
            }).$mount("#app");
        })
        .catch((v) => console.log(v));
});